package com.springboot.sba.D5_MySql.service;


import com.springboot.sba.D5_MySql.dao.LogRecordMapper;
import com.springboot.sba.bean.LogRecord;
import com.springboot.sba.bean.LogRecordExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/6/8 21:11
 */

public class MyService {
    @Autowired
    public LogRecordMapper logRecordMapper;
    List<LogRecord> list;
    boolean b=false;
    int n=0;

    @Scheduled(cron = "0/1 * * * * ?")
    public void test() {
        if(b){
            return;
        }
        for(int i=1;i<=1;i++){
            LocalDateTime time = LocalDateTime.of(2021, 10, 16, 02, 03, 30).minusDays(i);
            batchInsert(time);
        }
    }
    @Async
    public void batchInsert(LocalDateTime time1){
        b=true;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter ss = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

        for (int i = 1; i <= 200; i++) {
            LocalDateTime time=time1.plusMinutes(i*5);
            String record_time=dateTimeFormatter.format(time);
            String requestTime=ss.format(time);

            System.out.println(record_time);
            System.out.println(requestTime);

            list.forEach(x->{
                String uuid = UUID.randomUUID().toString().replace("-","");
                x.setRecordTime(record_time);
                x.setUuid(uuid);
                x.setRequestTime(requestTime);

            });




            Instant s = Instant.now();
            logRecordMapper.batchInsert(list);
            Instant e = Instant.now();
            Duration between = Duration.between(s, e);
            System.out.println("批量插入用时："+between.getSeconds());
        }

    }
    @PostConstruct
    public void getData(){
        Instant s = Instant.now();

         list = logRecordMapper.queryAll();
        Instant e = Instant.now();
        Duration between = Duration.between(s, e);
        System.out.println(between.getSeconds());
    }

}
